package com.summer.config;

import com.summer.interceptors.JWTinterceptos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;

/**
 *https://blog.csdn.net/catoop/article/details/50501696拦截器
 * https://blog.csdn.net/u012613251/article/details/83987585?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control&dist_request_id=1328769.575.16172671436278653&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control
 * &Description 拦截器配置
 *https://blog.csdn.net/u012862619/article/details/81557779?utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control&dist_request_id=&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control
 */
@Configuration
public class InterceptorsConfig extends WebMvcConfigurationSupport {//
    //implements WebMvcConfigurer
    @Bean
    //一定要讲拦截器的类装入bean中,并在注册拦截器的时候进行调用,不然自定义拦截器中无法@Autowired bean
    JWTinterceptos jwTinterceptos() {
        return new JWTinterceptos();
    }

    /*

     *&Description 配置添加拦截器

     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        System.out.println("1111");
        registry.addInterceptor(jwTinterceptos())
                .addPathPatterns("/**")  //拦截
                .excludePathPatterns("/test/**","/tb-userinfo/**","/tb-role/**","/tb-vote/**","/tb-score-prove/**")   //不拦截 "/tb-userinfo"
                .excludePathPatterns("/tb-account-bind/**","/tb-activityinfo/**","/tb-activity-type/**","/tb-apply/**","/tb-document/**")
                .excludePathPatterns("/tb-feedback/**","/tb-response/**","/tb-sign/**","/tb-sign-in/**","/userResponse/**")

                .excludePathPatterns("/users/**","/js/**","/css/**","favicon.icon","/doc.html")
                .excludePathPatterns("/swagger-resources/**","/webjars/**","/v2/**","favicon.icon","/swagger-ui.html/**");
        super.addInterceptors(registry);

    }

    /**
     * my test
     * @param registry
     *///https://blog.csdn.net/qq_39508627/article/details/104490268解决资源加载不到问题
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/js/**").addResourceLocations("classpath:/js/");
//        registry.addResourceHandler("swagger-ui.html")
//                .addResourceLocations("classpath:/META-INF/resources/");
//        registry.addResourceHandler("/webjars/**")
//                .addResourceLocations("classpath:/META-INF/resources/webjars/");
//    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");

//        //windows本地文件目录1
//           registry.addResourceHandler("/restaurantRes/**").addResourceLocations("file:C:/Users/Administrator/Desktop/restaurantRes/");

//        linux
        registry.addResourceHandler("/static/restaurantRes/**").addResourceLocations("file:/www/javaweb/propaganda_system/picture/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }


//    //拦截类型
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
//                .allowCredentials(true)
//                .allowedHeaders("*")
//                .allowedOrigins("*")
//                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
//                .maxAge(3600);
//    }


}
